import {
  ElMessage,
  ElMessageBox,
  ElNotification,
  ElLoading,
} from "element-plus";

/**
 * @des message提示框
 */
export const elMessage = (params = {}) => {
  ElMessage({
    grouping: true,
    duration: 2500,
    ...params,
  });
};

/**
 * 关闭message提示框信息
 */
export const clearElMessage = () => {
  ElMessage.closeAll();
};

/**
 * @des alert弹框
 */
export const elAlert = (params = {}) => {
  const autoParams = {
    title: "提示",
    confirmButtonText: "确定",
    closeOnClickModal: false,
    ...params,
  };
  ElMessageBox.alert(autoParams.message, autoParams.title, {
    ...autoParams,
    callback: () => {
      autoParams.confirm && autoParams.confirm();
    },
  });
};

/**
 * @des confirm确认弹框
 */
export const elConfirm = (params = {}) => {
  const autoParams = {
    title: "提示",
    message: "确定删除吗?",
    type: "warning",
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    closeOnClickModal: false,
    ...params,
  };
  ElMessageBox.confirm(autoParams.message, autoParams.title, {
    ...autoParams,
  })
    .then(() => {
      params.confirm && params.confirm();
    })
    .catch(() => {
      params.cancel && params.cancel();
    });
};

/**
 * @description 通知消息弹框
 */
export const elNotify = (params) => {
  const autoParams = {
    title: "提示",
    message: "消息通知",
    position: "top-right",
    showClose: false,
    duration: 3000,
    ...params,
  };
  ElNotification(autoParams);
};

/**
 * @description loading加载框
 */
let loadingInstance = null;
export const elLoading = (params) => {
  const autoParams = {
    lock: true,
    text: "加载中...",
    background: "rgba(0, 0, 0, 0.7)",
    ...params,
  };
  loadingInstance = ElLoading.service(autoParams);
};
export const clearElLoading = () => {
  loadingInstance && loadingInstance.close();
};
